﻿using NAutoForm.entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace NAutoForm.utils
{
    public class UserInfoGenerate
    {
        /// <summary>
        /// 生成随机年份
        /// </summary>
        /// <returns></returns>
        private static int GenerateYear()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int year = rd.Next(1965, DateTime.Now.Year);
            bu.Append(year);
            return int.Parse(bu.ToString());
        }

        /// <summary>
        /// 生成身份证号码
        /// </summary>
        /// <returns></returns>
        public static UserInfo GenerateUserInfo()
        {
            UserInfo userInfo = new UserInfo();
            // 找出地址和地区号码
            AddressGen.GetAddress(userInfo);
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            // 拼接地区号码
            bu.Append(userInfo.Userid);
            int year = GenerateYear();
            bu.Append(year);
            int month = rd.Next(1, 12);
            if (month < 10)
            {
                bu.Append(0);
            }
            bu.Append(month);
            int[] days;
            if (IsLeapYear(year))
            {
                days = new int[12] { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }
            else
            {
                days = new int[12] { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
            }

            int day = rd.Next(1, days[month]);
            if (day < 10)
            {
                bu.Append(0);
            }
            bu.Append(day);
            bu.Append(RandomCode());
            int gender = rd.Next(0, 9);
            if (gender % 2 == 0)
            {
                userInfo.UserGender = "女";
                userInfo.Name = ChineseNameUtils.GetWomanName();
            } 
            else
            {
                userInfo.UserGender = "男";
                userInfo.Name = ChineseNameUtils.GetManName();
            }
            bu.Append(gender);
            int[] c = { 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 };
            char[] r = { '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2' };
            string convert = bu.ToString();

            int it = 0;
            int res = 0;
            //  Console.WriteLine(res);
            while (it < 17)
            {
                res = res + (convert[it] - '0') * c[it];
                //   Console.WriteLine("第"+it+"次,"+convert[it]+"乘以"+c[it]);
                //    Console.WriteLine(res);
                it++;
            }
            int i = res % 11;
            bu.Append(r[i]);
            userInfo.Userid = bu.ToString();
            return userInfo;
        }

        /// <summary>
        /// 生成2位随机
        /// </summary>
        /// <returns></returns>
        private static string RandomCode()
        {
            StringBuilder bu = new StringBuilder();
            Random rd = new Random();
            int code = rd.Next(1, 99);
            if (code < 10)
            {
                bu.Append(0);
            }
            else if (code < 10)
            {
                bu.Append(0);
            }
            bu.Append(code);
            return bu.ToString();
        }


        /// <summary>
        /// 判断闰年
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        private static bool IsLeapYear(int year)
        {
            if (year % 4 == 0)
            {
                if (year % 100 == 0 && year % 400 != 0)
                {
                    return false;
                }
                return true;
            }
            return false;
        }
    }
}
